/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.SpdcfDAOInterface;
import com.wii.elpro.dao.hb.SpdcfDAOImpl;
import com.wii.elpro.entity.Spdcf;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.SpdcfEditAllForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author User
 */
public class SpdcfEditAction extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward edit(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
         SpdcfDAOInterface dao = new SpdcfDAOImpl();
         SpdcfEditAllForm f = (SpdcfEditAllForm)form;
         
        try{
            Spdcf spdcf = dao.getSpdcf(Long.parseLong(request.getParameter("id")));   
            f.setId(spdcf.getId());
            f.setAlamatPengiriman(spdcf.getAlamatPengiriman());
            f.setNote(spdcf.getNote());
            f.setTransportation(spdcf.getTransportation());
            f.setPpn(spdcf.getPpn());
            f.setValidityPrice(spdcf.getValidityPrice());
            f.setValidityStockLE(spdcf.getValidityStockLE());
            f.setProductionTime(spdcf.getProductionTime());
            f.setProductionTimeDays(spdcf.getProductionTimeDays());           
            f.setProductionCondition(spdcf.getProductionCondition());
            f.setFinishingDays(spdcf.getFinishingDays());
            f.setDeliveryMode(spdcf.getDeliveryMode());
            f.setDeliveryModeDesc(spdcf.getDeliveryModeDesc());
            f.setDeliveryTime(spdcf.getDeliveryTime());     
            f.setDeliveryTimeDays(spdcf.getDeliveryTimeDays());
            request.setAttribute("noQuatation", spdcf.getInquery().getNoQuotation());
            request.setAttribute("customer",spdcf.getCustomer().getNama());
        }catch(Exception e){
            e.printStackTrace();
        }
        return mapping.findForward("edit");
    } 
    public ActionForward update(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
           {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        SpdcfDAOInterface dao = new SpdcfDAOImpl();
        SpdcfEditAllForm f = (SpdcfEditAllForm)form;
        try{
            Spdcf spdcf = dao.getSpdcf(Long.parseLong(request.getParameter("id")));
            spdcf.setAlamatPengiriman(f.getAlamatPengiriman());
            spdcf.setNote(f.getNote());
            spdcf.setTransportation(f.getTransportation());
            spdcf.setPpn(f.getPpn());
            spdcf.setValidityPrice(f.getValidityPrice());
            spdcf.setValidityStockLE(f.getValidityStockLE());
            spdcf.setProductionTime(f.getProductionTime());
            spdcf.setProductionTimeDays(f.getProductionTimeDays());
            spdcf.setProductionCondition(f.getProductionCondition());
            spdcf.setFinishingDays(f.getFinishingDays());
            spdcf.setDeliveryMode(f.getDeliveryMode());
            spdcf.setDeliveryModeDesc(f.getDeliveryModeDesc());
            spdcf.setDeliveryTime(f.getDeliveryTime());
            spdcf.setDeliveryTimeDays(f.getDeliveryTimeDays());
            dao.update(spdcf);
        }catch(Exception e){
            e.printStackTrace();
        }
        return mapping.findForward("update");
    }
    
     public ActionForward delete(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        SpdcfDAOInterface dao = new SpdcfDAOImpl();
        try{
            Spdcf spdcf = dao.getSpdcf(Long.parseLong(request.getParameter("id")));
            dao.delete(spdcf);
        }catch(Exception e){
            e.printStackTrace();
        }
        return mapping.findForward("delete");
    }
}